package org.geektimes.configuration.microprofile.config.converter;

import org.apache.commons.configuration2.convert.DefaultConversionHandler;
import org.eclipse.microprofile.config.spi.Converter;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @Author: menglinggang
 * @Date: 2021-03-17
 * @Time: 1:29 下午
 */
public class StringToDateConverter implements Converter<Date>{

    private DefaultConversionHandler defaultConversionHandler;

    public StringToDateConverter(){
        defaultConversionHandler = new DefaultConversionHandler();
    }

    @Override
    public Date convert(String s) throws IllegalArgumentException, NullPointerException {
        defaultConversionHandler.setDateFormat("yyyy-mm-dd hh:MM:ss");
        return defaultConversionHandler.to(s, Date.class, null);
    }
}
